package com.dk.service.comment;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.pojo.comment.Comment;
import com.dk.pojo.user.TbUser;
import com.dk.utils.PageInfo;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public int saveComment(Comment comment, TbUser user) {
        comment.setUserId(user.getId());
        comment.setUsername(user.getUsername());
        mongoTemplate.insert(comment);
        return 1;
    }

    @Override
    public PageInfo<Comment> getComment(Integer pageNumber, Long goodsId) {
        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageNumber(pageNumber);
        pageInfo.setPageSize(3);
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.where("goodsId").is(goodsId);
        query.addCriteria(criteria);
        long count = mongoTemplate.count(query, Comment.class);
        pageInfo.setTotal(count);
        query.skip(pageInfo.getOffset()).limit(pageInfo.getPageSize());
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        pageInfo.setList(commentList);
        return pageInfo;
    }
}
